package com.atguigu.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.web.admin.mapper.*;
import com.atguigu.web.admin.service.*;
import com.atguigu.web.admin.vo.attr.AttrValueVo;
import com.atguigu.web.admin.vo.graph.GraphVo;
import com.atguigu.web.admin.vo.room.RoomDetailVo;
import com.atguigu.web.admin.vo.room.RoomItemVo;
import com.atguigu.web.admin.vo.room.RoomQueryVo;
import com.atguigu.web.admin.vo.room.RoomSubmitVo;
import com.atguigu.model.entity.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public void removeById(Long id) {
//        1、先删除房间信息
        removeById(id);
//        2.删除该房间id下的图片信息列
//        新建通过房间id查找图片信息列的条件查询器
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        因为图片列表绑定了公寓和房间两个表，所以先确定对象类型（得是房间），再确定对象id（和输入id号相同）
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

//        3.删除该房间下的属性值列
        LambdaQueryWrapper<RoomAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        attrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(attrValueLambdaQueryWrapper);

//        4.删除该房间id下的配套设置列
        LambdaQueryWrapper<RoomFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(facilityLambdaQueryWrapper);

//        5.删除该房间id下的标签信息列
        LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(labelLambdaQueryWrapper);

        // 6.删除该房间id下的支付类型列
        LambdaQueryWrapper<RoomPaymentType> paymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(paymentTypeLambdaQueryWrapper);

        // 7.删除该房间id下的租期列
        LambdaQueryWrapper<RoomLeaseTerm> leaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(leaseTermLambdaQueryWrapper);


    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page,queryVo);
    }

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
//        如果controller层调入的roomsubmitVo有roomID，说明是保存信息。
        boolean isUpdate = roomSubmitVo.getId() != null;
//        既然是新存room信息，直接调用父类ServiceImpl的saveOrUpdate方法
        super.saveOrUpdate(roomSubmitVo);

//       既然不是新存room信息，而是更新，就要先删除旧的room各项属性、配套、标签等信息，
//        再插入新的配套、属性、标签等信息
        if (isUpdate) {
//            1.删除原有的房间图片列，先弄个Labmda条件查询器，把该roomID下原有的房间图片列找到
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //先找到图片列中ItemType属性为枚举值Room的
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            //再找到图片列中ItemId属性为用户提交的roomsubmitVo信息单中的id的
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            //调用图片信息Service中的移除功能（它继承了Iservice有CRUD方法），去删除符合条件的旧图片信息列
            graphInfoService.remove(graphInfoLambdaQueryWrapper);


//           2.删除房屋原有的属性值列表，也是先弄个Labmda条件查询器，把该roomId下有的属性列找到
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

//            3.删除原有的房间配套列表，也是先弄个Labmda条件查询器，把该roomId下有的配套列找到
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

//            4.删除原有的房间标签列表，和配套设施一样，也是先弄个Labmda条件查询器，把该roomId下有的配套列找到
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);

//            5.删除原有的支付方式列表，和配套设施一样，也是先弄个Labmda条件查询器，把该roomId下有的支付列找到
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

//            6.删除原有的租期列表，和支付方式列表一样，也是先弄个Labmda条件查询器，把该roomId下有的租期列找到
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }

//        ②删除完旧的列表后，开始插入新的信息列表
//        1、保存新的图片列
        //先从用户提交的信息列表中得到图片信息列，装成一个单独的List
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
//            创建一个装图片信息的新list
            ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
//               将图片信息list的每个信息绑定到GraphInfo上
                GraphInfo graphInfo = new GraphInfo();
//                先把Itemtype设置为room类型，这样属性就不会和房间撞
                graphInfo.setItemType(ItemType.ROOM);
//                用户提交的信息单中的房间id设置给这个图片信息的所属对象id
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphInfo.getUrl());

            //将用户提交的roomSubmitVo表中的每张GraphVo单拎出来弄成GraphVoList，
            //赋予GraphInfo再挨个装进动态数组，形成图片信息合集。
                graphInfoArrayList.add(graphInfo);
//                将图片信息合集用图片信息Service层的方法保存起来
                graphInfoService.saveBatch(graphInfoArrayList);


//                2.保存新的房间属性值列表(没懂这步在干嘛）
                //把用户上传的房屋信息列表中的属性id全掏出来组成一个集合
                List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
                if (!CollectionUtils.isEmpty(attrValueIds)) {
//                    弄个arraylist用来装房间属性值
                    ArrayList<RoomAttrValue> roomAttrValueArrayList = new ArrayList<>();
                    for (Long attrValueId : attrValueIds) {
//                        构建者模式，根据每个属性值id构建出一个房屋属性值对象
                        RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                                .roomId(roomSubmitVo.getId()).attrValueId(attrValueId)
                                .build();
//                        把构造出来的房屋属性值对象装进一个列表里
                        roomAttrValueArrayList.add(roomAttrValue);
                    }
//                    调用房屋属性值的Service方法，把这批房屋属性值对象列表保存到sql里
                    roomAttrValueService.saveBatch(roomAttrValueArrayList);
                }


//                3.保存新的配套设施信息列表
                //把用户上传的配套设施信息列表中的id全掏出来组成一个集合
                List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
                if (!CollectionUtils.isEmpty(facilityInfoIds)) {
//                    弄个arraylist用来装配套属性值
                    ArrayList<RoomFacility> roomFacilityArrayList = new ArrayList<>();
                    for (Long facilityInfoId : facilityInfoIds) {
//                        构建者模式，根据每个配套值id构建出一个房间配套值对象
                        RoomFacility roomFacility = RoomFacility.builder()
                                .roomId(roomSubmitVo.getId()).facilityId(facilityInfoId)
                                .build();
//                        把构造出来的房间配套值对象装进一个列表里
                        roomFacilityArrayList.add(roomFacility);
                    }
//                    调用房屋属性值的Service方法，把这批房屋属性值对象列表保存到sql里
                    roomFacilityService.saveBatch(roomFacilityArrayList);
                }


//                4.保存新的标签信息列表
                //把用户上传的标签信息列表中的id全掏出来组成一个集合
                List<Long> LabelInfoIds = roomSubmitVo.getLabelInfoIds();
                if (!CollectionUtils.isEmpty(LabelInfoIds)) {
//                    弄个arraylist用来装配套属性值
                    ArrayList<RoomLabel> roomLabelArrayList = new ArrayList<>();
                    for (Long LabelInfoId : LabelInfoIds) {
//                        构建者模式，根据每个配套值id构建出一个房间配套值对象
                        RoomLabel roomLabel = RoomLabel.builder()
                                .roomId(roomSubmitVo.getId()).labelId(LabelInfoId)
                                .build();
//                        把构造出来的房间配套值对象装进一个列表里
                        roomLabelArrayList.add(roomLabel);
                    }
//                    调用房屋属性值的Service方法，把这批房屋属性值对象列表保存到sql里
                    roomLabelService.saveBatch(roomLabelArrayList);
                }


//                5.保存新的支付方式列表
                //把用户上传的支付方式列表中的id全掏出来组成一个集合
                List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
                if (!CollectionUtils.isEmpty(paymentTypeIds)) {
//                    弄个arraylist用来装支付方式对象
                    ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
//                    根据列表支付方式id列表中的每个id创造出对应的支付方式对象
                    for (Long paymentTypeId : paymentTypeIds) {
//                        构建者模式，根据每个配套值id构建出一个房间配套值对象
                        RoomPaymentType roompaymentType = RoomPaymentType.builder()
                                .roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId)
                                .build();
//                        把构造出来的房间配套值对象装进一个列表里
                        roomPaymentTypeList.add(roompaymentType);
                    }
//                    调用房屋属性值的Service方法，把这批房屋属性值对象列表保存到sql里
                    roomPaymentTypeService.saveBatch(roomPaymentTypeList);
                }


//                6.保存新的租期列表
                //把用户上传的租期列表中的id全掏出来组成一个集合
                List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
                if (!CollectionUtils.isEmpty(leaseTermIds)) {
//                    //弄个arraylist用来装房间的可选租期对象
                    ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
//                    根据列表可选租期id列表中的每个id创造出对应的支付方式对象
                    for (Long leaseTermId : leaseTermIds) {
//                        构建者模式，根据每个配套值id构建出一个房间配套值对象
                        RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                                .roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId)
                                .build();
//                        把构造出来的房间配套值对象装进一个列表里
                        roomLeaseTerms.add(roomLeaseTerm);
                    }
//                    调用房屋属性值的Service方法，把这批房屋属性值对象列表保存到sql里
                    roomLeaseTermService.saveBatch(roomLeaseTerms);
                }

            }

        }
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {//根据房间id查询房间详情

//        1.根据房间id查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
//        2.根据房间信息查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
//        3.根据id查询房间的图片信息列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM,id);
//        4.查询该房间的属性值列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
//        5.根据id房间的配套设施列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
//        6.根据id查询房间的标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
//        7.根据id查询房间支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
//        8.根据id查询租期方式列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();//新建RoomDetailVo类型的对象
        BeanUtils.copyProperties(roomInfo,roomDetailVo);//将roomInfo中的属性值复制到roomDetailVo
//        以便返回给前端

//        将根据房间id查询到的各类信息（房间信息、图片信息...)塞进RoomDetailVo这个对象，以便返给前端
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

//        将装载好了将根据房间id查询到的各类信息（房间信息、图片信息...)组成的RoomDetailVo这个对象返给前端
        return roomDetailVo;




    }
}





